package com.tsd.system.controller;

import com.alibaba.fastjson.JSON;
import com.tsd.CommonMsgNameConstants;
import com.tsd.CommonParamNameConstants;
import com.tsd.common.controller.BaseController;
import com.tsd.core.constants.Constants;
import com.tsd.core.datasource.DataSourceContextHolder;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.utils.HttpUtil;
import com.tsd.core.vo.ErrorCode;
import com.tsd.core.vo.HlpException;
import com.tsd.core.vo.HttpResult;
import com.tsd.system.entity.SysAccountSetResourceExt;
import com.tsd.system.entity.SysUser;
import com.tsd.system.service.SysAccountSetResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 描述：SysAccountSetResource资源关系表 控制层
 *
 * @author huawei
 * @date 2022/03/05 11:49:46
 */
@Controller
@Scope("prototype")
@RequestMapping("/api/v2")
@Api(tags = "系统账套-资源关系表", description = "资源关系表的API描述")
public class SysAccountSetResourceController extends BaseController {

    @Resource
    private SysAccountSetResourceService sysAccountSetResourceService;

    @ApiOperation(value = "保存资源关系表", notes = "保存资源关系表，具有id值则更新，没有id值则新增")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "obj", value = "资源关系表", required = true, paramType = "body", dataType = "SysAccountSetResource")
    })
    @ResponseBody
    @RequestMapping(value = "/saveSysAccountSetResources.action", method = RequestMethod.POST)
    public HttpResult<String> saveSysAccountSetResources() {
        HttpResult<String> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            String account_set_sid = HttpUtil.getAccountSet(request);
            super.checkEmpty(account_set_sid, CommonMsgNameConstants.getEmptyMsg(Constants.HEADER_NAME_ACCOUNT_SET_SID));
            Object list = map.get(CommonParamNameConstants.LIST);
            if (list != null) {
                String json = JSON.toJSONString(list);
                List<SysAccountSetResourceExt> resList = JSON.parseArray(json, SysAccountSetResourceExt.class);
                DataSourceContextHolder.setDataSourceType(null);
                sysAccountSetResourceService.saveSysAccountSetResources(account_set_sid, resList, opUser);
                hr.setCode(ErrorCode.CODE_SUCCESS);
            } else {
                throw new HlpException(CommonMsgNameConstants.getEmptyMsg(CommonParamNameConstants.LIST));
            }
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "根据account_set_sid查询资源关系表", notes = "根据account_set_sid查询资源关系表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account_set_sid", value = "名称", required = false, dataType = "string", paramType = "form"),
    })
    @ResponseBody
    @RequestMapping(value = "/findSysAccountSetResourcesByAccountSetSid.action", method = RequestMethod.POST)
    public HttpResult<List<SysAccountSetResourceExt>> findSysAccountSetResourcesByAccountSetSid() {
        HttpResult<List<SysAccountSetResourceExt>> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            String account_set_sid = HttpUtil.getAccountSet(request);
            if (HlpUtils.isEmpty(account_set_sid)){
                throw new HlpException(CommonMsgNameConstants.getEmptyMsg(Constants.HEADER_NAME_ACCOUNT_SET_SID));
            }
            List<SysAccountSetResourceExt> list = sysAccountSetResourceService.findSysAccountSetResourcesByAccountSetSid(account_set_sid);
            hr.setData(list);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "根据account_set_sid查询资源关系表", notes = "根据account_set_sid查询资源关系表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account_set_sid", value = "名称", required = false, dataType = "string", paramType = "form"),
    })
    @ResponseBody
    @RequestMapping(value = "/findSysAccountSetResources4Setting.action", method = RequestMethod.POST)
    public HttpResult<List<SysAccountSetResourceExt>> findSysAccountSetResources4Setting() {
        HttpResult<List<SysAccountSetResourceExt>> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            String account_set_sid = HttpUtil.getAccountSet(request);
            if (HlpUtils.isEmpty(account_set_sid)){
                throw new HlpException(CommonMsgNameConstants.getEmptyMsg(Constants.HEADER_NAME_ACCOUNT_SET_SID));
            }
            List<SysAccountSetResourceExt> list = sysAccountSetResourceService.findSysAccountSetResources4Setting(account_set_sid);
            hr.setData(list);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }
}
